#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

typedef struct ListNode {
	unsigned short val;
	struct ListNode* next;
} ListNode, *ListNodePointer;

typedef struct {
	ListNodePointer head;
	size_t _size;
} MyLinkedList;

int main()
{
	return 0;
}

MyLinkedList* myLinkedListCreate()
{
	MyLinkedList* obj = malloc(sizeof(MyLinkedList));
	obj->head = calloc(1, sizeof(ListNode));
	obj->_size = 0;
	return obj;
}

int myLinkedListGet(MyLinkedList* obj, int index)
{
    printf("%d\t", obj->_size);
	if (index + 1 > obj->_size)
		return -1;

	ListNodePointer p = obj->head;
	while (index--)
		p = p->next;
    printf("%d\t", p->val);
	return p->next->val;
}

void myLinkedListAddAtHead(MyLinkedList* obj, int val)
{
	obj->_size++;
    printf("%d\t", obj->_size);
	ListNodePointer head = obj->head;
	ListNodePointer new = malloc(sizeof(ListNode));
	new->val = val;
	new->next = head->next;
	head->next = new;
}

void myLinkedListAddAtTail(MyLinkedList* obj, int val)
{
	int size = obj->_size++;
    printf("%d\t", obj->_size);
	ListNodePointer p = obj->head;
	while (size--) p = p->next;
	ListNodePointer new = malloc(sizeof(ListNode));
	new->val = val;
	new->next = 0;
	p->next = new;
}

void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val)
{
	if (index < 0) index = 0;
	int size = obj->_size;
	if (index > size) return;
	obj->_size++;
    printf("%d\t", obj->_size);
	ListNodePointer p = obj->head;
	while (index--) p = p->next;
	ListNodePointer new = malloc(sizeof(ListNode));
	new->val = val;
	new->next = p->next;
	p->next = new;
}

void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index)
{
	int size = obj->_size;
	if (index < 0 || index > size - 1) return;

	obj->_size--;
    printf("%d\t", obj->_size);
	ListNodePointer p = obj->head;
	while (index--) p = p->next;

	ListNodePointer del = p->next;
	p->next = del->next;
	free(del);
}

void myLinkedListFree(MyLinkedList* obj)
{
	ListNodePointer p = obj->head;
	ListNodePointer p_next;
	while (p) {
		p_next = p->next;
		free(p);
		p = p_next;
	}
	free(obj);
}

/**
 * Your MyLinkedList struct will be instantiated and called as such:
 * MyLinkedList* obj = myLinkedListCreate();
 * int param_1 = myLinkedListGet(obj, index);

 * myLinkedListAddAtHead(obj, val);

 * myLinkedListAddAtTail(obj, val);

 * myLinkedListAddAtIndex(obj, index, val);

 * myLinkedListDeleteAtIndex(obj, index);

 * myLinkedListFree(obj);
*/
